package view.tool;

import controller.SequenceChangeListener;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.*;
import model.Instrument;
import util.Images;

/**
 * Tool for changing properties of a Sequence.
 * Currently it can change the volume, the type of Instrument used and also
 * remove the Sequence from the Piece.
 */
public class SequenceTool extends Tool {

	private JButton removeButton;
	private JSlider volumeSlider;
	private JButton[] instrumentButtons;

	public SequenceTool(int initialVolume) {
		this.setLayout(new BorderLayout(4,4));
		this.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));

		JPanel controlPanel = new JPanel(new GridLayout(1,0,4,0));
		controlPanel.setOpaque(false);

		JPanel volumePanel = new JPanel(new BorderLayout());
		volumePanel.setOpaque(false);

	//Volume Slider
		volumeSlider = new JSlider(SwingConstants.VERTICAL, 0, 200, initialVolume);
		volumeSlider.setMajorTickSpacing(100);
		volumeSlider.setPaintTicks(true);
		volumeSlider.setOpaque(false);
		volumeSlider.setName(SequenceChangeListener.VOLUME);
		JLabel volumeLabel = new JLabel("Volume");
		volumeLabel.setLabelFor(volumeSlider);
		volumeLabel.setForeground(Color.WHITE);

		volumePanel.add(volumeSlider, BorderLayout.CENTER);
		volumePanel.add(volumeLabel, BorderLayout.SOUTH);

		controlPanel.add(volumePanel);

	//Instrument List
		JPanel standardInstrumentPanel = new JPanel(new GridLayout(1,0));
		JPanel funInstrumentPanel = new JPanel(new GridLayout(1,0));
		standardInstrumentPanel.setOpaque(false);
		funInstrumentPanel.setOpaque(false);

		Instrument[] instruments = Instrument.values();
		instrumentButtons = new JButton[instruments.length];
		for(int i = 0; i < instruments.length; i++) {
			String name = instruments[i].toString();
			JButton instrumentButton = new JButton(
				name.charAt(0) + name.substring(1).toLowerCase(),
				new ImageIcon(Images.getInstrumentIcon(instruments[i]))
			);
			instrumentButton.setMargin(new Insets(1,1,1,1));
			instrumentButton.setHorizontalTextPosition(SwingConstants.CENTER);
			instrumentButton.setVerticalTextPosition(SwingConstants.BOTTOM);
			instrumentButton.setActionCommand(instruments[i].toString());

			instrumentButton.setBorderPainted(false);

			//Seperate "Fun" and Standard instrument for easier access.
			instrumentButtons[i] = instrumentButton;
			if(instruments[i].isFun()) {
				funInstrumentPanel.add(instrumentButton);
			} else {
				standardInstrumentPanel.add(instrumentButton);
			}

		}
		JScrollPane standardInstrumentScrollPane = new JScrollPane(standardInstrumentPanel,
			ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
			ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED
		);
		JScrollPane funInstrumentScrollPane = new JScrollPane(funInstrumentPanel,
			ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
			ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED
		);

		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
		tabbedPane.addTab("S", null, standardInstrumentScrollPane, "Standard Instruments");
		tabbedPane.addTab("F", null, funInstrumentScrollPane, "Fun Instruments");

	//Remove Button
		removeButton = new JButton(new ImageIcon(Images.getDeleteIcon()));
		removeButton.setActionCommand(SequenceChangeListener.DELETE_SEQUENCE);

		this.add(controlPanel, BorderLayout.WEST);
		this.add(tabbedPane, BorderLayout.CENTER);
		this.add(removeButton, BorderLayout.EAST);
	}

	public void addSequenceChangeListener(SequenceChangeListener scl) {
		removeButton.addActionListener(scl);
		volumeSlider.addChangeListener(scl);
		for(JButton instrumentButton : instrumentButtons) {
			instrumentButton.addActionListener(scl);
		}
	}

}
